<?php
/**
 * Description: 行测速算模块
 * User: devforma
 * Date: 16/7/26
 * Time: 11:24
 */

require_once __DIR__ . '/ProcessExerciseBase.class.php';

class SusuanModule extends ProcessExerciseBase {
    /**
     * 设置作业类型
     */
    protected function setExerciseType() {
        $this->data['exercise_type'] = 31;
    }

    /**
     * 设置作业题目内容
     */
    protected function setExerciseQuestion() {
        parent::setExerciseQuestion();
    }

    /**
     * 设置作业记录的状态
     */
    protected function setSubmitStatus() {
        if (empty($this->submitArgs['done'])) {
            $this->submitExerciseRecord['status'] = 0; //未完成
        } else {
            if ($this->exerciseInfo->end_time > $this->now)
                $this->submitExerciseRecord['status'] = 1; //已完成
            else
                $this->submitExerciseRecord['status'] = 5; //过期完成
        }
    }


    /**
     * 设置提交权限
     */
    protected function setSubmitPriv() {
        parent::setSubmitPriv();
    }

    /**
     * 设置用户答案
     *
     */
    protected function setUserAnswer() {
        $summary = json_decode($this->userExerciseRecord->summary, true);

        $this->data['user_answer'] = [];
        for ($i = 1; $i <= 6; $i++) {
            $key = 'level_'.$i;
            if (isset($summary[$key])) {
                $this->data['user_answer'][$key] = [
                    'duration'      => (int)$summary[$key]['duration'],
                    'accuracy'      => round($summary[$key]['right']/$summary[$key]['total'], 3),
                    'avg_duration'  => round($summary[$key]['duration']/$summary[$key]['total']),
                    'finish'        => $summary[$key]['right'] == $summary[$key]['total']
                ];
            }
        }
    }

    /**
     * 校验参数
     *
     * @param array $args 参数
     * @return bool
     */
    public function validateArgs($args) {
        if (!empty($args['level']) && isset($args['right']) && !empty($args['total']) && isset($args['duration'])) {
            $this->submitArgs = $args;
            return true;
        }

        return false;
    }

    /**
     * 保存答题记录记作业统计信息
     */
    protected function saveQuestionRecordsAndSetSubmitSummary() {

        $key = 'level_'.$this->submitArgs['level'];
        if (!empty($this->userExerciseRecord)) {
            $summary = json_decode($this->userExerciseRecord->summary, true);
        } else {
            $summary = [];
        }

        //更新该级别速算的统计信息,只有在第一次做或正确率高于当前才会更新
        $update = false;
        if (!isset($summary[$key])) { //第一次做
            $update = true;
        } elseif ($summary[$key]['right']/$summary[$key]['total'] < $this->submitArgs['right']/$this->submitArgs['total']) { //正确率比已有的高
            $update = true;
        } elseif ($summary[$key]['right'] == $this->submitArgs['right'] && $summary[$key]['duration'] > $this->submitArgs['duration']) { //正确率相等,时间比已有的短
            $update = true;
        }

        if ($update) {
            $summary[$key] = [
                'right'     => $this->submitArgs['right'],
                'total'     => $this->submitArgs['total'],
                'duration'  => $this->submitArgs['duration']
            ];

            //第六关全对则标记为已完成
//            if ($summary['level_6']['right'] == $summary['level_6']['total']) {
//                if ($this->exerciseInfo->end_time > $this->now)
//                    $this->submitExerciseRecord['status'] = 1; //已完成
//                else
//                    $this->submitExerciseRecord['status'] = 5; //过期完成
//            }
        }

        $this->submitExerciseRecord['summary'] = json_encode($summary);
    }

    /**
     * 获取自测题目
     *
     * @param array $qids 题目id
     * @param array $userAnswers
     * @return null
     */
    protected function getSelfTestQuestions($qids, &$userAnswers) {
        return parent::getSelfTestQuestions($qids, $userAnswers);
    }

    /**
     * 题号速算自测题目信息
     *
     * @param array $args
     * @return null
     */
    public function submitSelfTest($args) {

    }
}